package com.aos.erp.schools.rest.common;

import com.aos.erp.common.GlobalConstant;
import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.schools.enums.student.IsDel;
import com.aos.erp.schools.enums.student.RegionType;
import com.aos.erp.schools.model.common.Region;
import com.aos.erp.schools.model.common.vo.RegionTree;
import com.aos.erp.schools.model.common.vo.RegionVo;
import com.aos.erp.schools.model.sys.vo.MenuTree;
import com.aos.erp.schools.service.common.RegionService;
import com.aos.erp.schools.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: erp
 * @description: 区域字典
 * @author: tanzhiyu
 * @create: 2018-11-09 17:52
 **/
@Api(value = "区域字典", tags = {"区域字典API"})
@RestController("RESTRegionController")
@RequestMapping("/api/captcha")
public class RegionController {
    @Resource
    private RegionService regionService;

    @ApiOperation(value = "获取国家")
    @PostMapping("/getRegionCountry")
    public ResultEntity getRegionCountry() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("leveltype", RegionType.country.getValue());
        List<Region> list = regionService.findByParams(map);
        return ResultEntity.success(list, "获取省数据");
    }

    @ApiOperation(value = "获取省")
    @PostMapping("/getRegionProvince")
    public ResultEntity getRegionProvince() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("leveltype", RegionType.province.getValue());
        List<Region> list = regionService.findByParams(map);
        return ResultEntity.success(list, "获取省数据");
    }

    @ApiOperation(value = "获取省(忽略)")
    @PostMapping(GlobalConstant.GATEWAY_IGNORE+"/getRegionProvince")
    public ResultEntity getRegionProvinceHl() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("leveltype", RegionType.province.getValue());
        List<Region> list = regionService.findByParams(map);
        return ResultEntity.success(list, "获取省数据");
    }


    @ApiOperation(value = "获取城市")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "城市code", required = false, dataType = "String", paramType = "Query")
    })
    @PostMapping("/getRegionCity")
    public ResultEntity getRegionCity(String code) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("leveltype", RegionType.city.getValue());
        map.put("code", code);
        List<RegionVo> list = regionService.findByParamsParentId(map);
        return ResultEntity.success(list, "获取区县数据");
    }

    @ApiOperation(value = "获取城市(忽略)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "城市code", required = false, dataType = "String", paramType = "Query")
    })
    @PostMapping(GlobalConstant.GATEWAY_IGNORE+"/getRegionCity")
    public ResultEntity getRegionCityHl(String code) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("leveltype", RegionType.city.getValue());
        map.put("code", code);
        List<RegionVo> list = regionService.findByParamsParentId(map);
        return ResultEntity.success(list, "获取区县数据");
    }


    @ApiOperation(value = "获取区县")
    @PostMapping("/getRegionArea")
    public ResultEntity getRegionArea() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("leveltype", RegionType.area.getValue());
        List<Region> list = regionService.findByParams(map);
        return ResultEntity.success(list, "获取区县数据");
    }

    @ApiOperation(value = "获取区县(忽略)")
    @PostMapping(GlobalConstant.GATEWAY_IGNORE+"/getRegionArea")
    public ResultEntity getRegionAreaHl() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("leveltype", RegionType.area.getValue());
        List<Region> list = regionService.findByParams(map);
        return ResultEntity.success(list, "获取区县数据");
    }

    @ApiOperation(value = "获取省市区下级区域")
    @PostMapping("/getRegion")
    public ResultEntity getRegion(String parentId) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("parentId", parentId);
        List<Region> list = regionService.findByParams(map);
        return ResultEntity.success(list, "获取省市区下级区域");
    }

    @ApiOperation(value = "根据区域组织树")
    @GetMapping("/getRegionTree")
    public ResultEntity getRegionTree() throws Exception {
        List<RegionTree> regionTree = regionService.selectTree();
        return ResultEntity.success(regionTree);
    }

    @ApiOperation(value = "根据城市code获取下级信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "城市code", required = false, dataType = "String", paramType = "Query")
    })
    @PostMapping("/getRegionCode")
    public ResultEntity getRegionCode(String code) throws Exception {
        if (StringUtils.isEmpty(code)) {
            return ResultEntity.failed("城市code不能为空", ResultEntity.CODE_NULL);
        }
        List<Region> list = regionService.findByParamsCode(code);
        return ResultEntity.success(list, "获取区县数据");
    }
}
